<?php
/**
 * 获取关键字接口
 */
defined('IN_PHPCMS') or exit('No permission resources.'); 
//自带接口以及无法使用
//define('API_URL_GET_KEYWORDS', 'http://tool.phpcms.cn/api/get_keywords.php');
define('API_URL_GET_KEYWORDS', 'http://zhannei.baidu.com/api/customsearch/keywords');

$number = intval($_GET['number']);
$data = $_POST['data'];
echo get_keywords($data, $number);

function get_keywords($data, $number = 3) {
	$data = trim(strip_tags($data));
    if(empty($data)) return '';
	//$http->post(API_URL_GET_KEYWORDS, array('siteurl'=>SITE_URL, 'charset'=>CHARSET, 'data'=>$data, 'number'=>$number));
	$rs = pc_file_get_contents(API_URL_GET_KEYWORDS.'?title=' . urlencode($data), 3);
    if (!$rs) {
        return '';
    }
	$data = json_decode($rs, true);
    if (!$data || empty($data['result']['res']['keyword_list'])) {
        return '';
    }
    if (count($data['result']['res']['keyword_list']) > $number) {
        $data['result']['res']['keyword_list'] = array_slice($data['result']['res']['keyword_list'], 0, $number);
    }
    if (CHARSET != 'utf-8') {
        return iconv('utf-8', 'gbk', implode(' ', $data['result']['res']['keyword_list']));
    } else {
        return implode(' ', $data['result']['res']['keyword_list']);
    }
}
?>